import { _decorator, Component, Node, Sprite, Vec3,PhysicsSystem2D } from 'cc';
import { loading } from './loading';
import { store } from './store';
import { UITransform } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ground')
export class ground extends Component {
    @property(Node)
    public ground1:Node = null
    @property(Node)
    public ground2:Node = null
    // 移动速度
    @property(Number)
    public speed:number = 0
    update(deltaTime: number) {
      // 游戏状态不是结束才移动
      if(loading.gameState){
         // 移动背景
         this.ground1.setPosition(new Vec3( this.ground1.getPosition().x - store.speed * deltaTime ,this.ground1.getPosition().y, 0))
         this.ground2.setPosition(new Vec3( this.ground2.getPosition().x - store.speed * deltaTime ,this.ground2.getPosition().y, 0))
         
         // 检查是否超出屏幕
         // 计算宽度
         const bgWidth = this.ground1.getComponent(UITransform).contentSize.width * this.ground1.scaleX
         if (this.ground1.getPosition().x <= -bgWidth) {
            this.ground1.setPosition(new Vec3( this.ground2.getPosition().x + bgWidth ,this.ground1.getPosition().y, 0))
         }
         if (this.ground2.getPosition().x <= -bgWidth) {
            this.ground2.setPosition(new Vec3( this.ground1.getPosition().x + bgWidth ,this.ground1.getPosition().y, 0))
         }
      }
    }
}


